package utils

import (
	"file_sys_server/model"
	"fmt"

	"github.com/tealeg/xlsx"
)

// 转换excel文本为bean 类型
func ConvertSampleToBean() (mBean []*model.Sample_bean_mfdcg, err error) {
	// 打开文件
	filePath := "./files/excels/样品列表.xlsx"
	xlfile, err := xlsx.OpenFile(filePath)

	if err != nil {
		fmt.Println("文件打开失败" + err.Error())
		return nil, err
	}

	// 读取数据
	cvtBeans := make([]*model.Sample_bean_mfdcg, 0)
	for _, sheet := range xlfile.Sheets {
		for index, row := range sheet.Rows { // 行
			if index == 0 { // 标题行跳过
				continue
			}
			cells := row.Cells // 行各元素
			sampleRecord := &model.Sample_bean_mfdcg{
				SampleName:    cells[0].String(),
				RelateProject: cells[1].String(),
				SampleCode:    cells[2].String(),
				Limit:         cells[3].String(),
				PreStr1:       cells[4].String(),
			}
			cvtBeans = append(cvtBeans, sampleRecord)
		}
	}

	return cvtBeans, nil
}

// 转换excel文本为bean 类型 检测项目转换
func ConvertProjectToBean() (mBean []*model.Project_bean_mfdcg, err error) {
	// 打开文件
	filePath := "./files/excels/项目列表.xlsx"
	xlfile, err := xlsx.OpenFile(filePath)

	if err != nil {
		fmt.Println("文件打开失败" + err.Error())
		return nil, err
	}

	// 读取数据
	cvtBeans := make([]*model.Project_bean_mfdcg, 0)
	for _, sheet := range xlfile.Sheets {
		for index, row := range sheet.Rows { // 行
			if index == 0 { // 标题行跳过
				continue
			}
			cells := row.Cells // 行各元素
			testMethod, _ := cells[1].Int()
			judge, _ := cells[2].Float()
			projectRecord := &model.Project_bean_mfdcg{
				ProjectName: cells[0].String(),
				TestMethod:  testMethod,
				JudgeThresh: judge,
				Unint:       cells[3].String(),
				Limit:       cells[4].String(),
				QrcodeStr:   cells[5].String(),
			}
			cvtBeans = append(cvtBeans, projectRecord)
		}
	}

	return cvtBeans, nil
}

// 转换excel文本为bean 类型 多联卡项目转换
func ConvertUnionProjectToBean() (mBean []*model.Union_project_bean_mfdcg, err error) {
	// 打开文件
	filePath := "./files/excels/多联卡项目.xlsx"
	xlfile, err := xlsx.OpenFile(filePath)

	if err != nil {
		fmt.Println("文件打开失败" + err.Error())
		return nil, err
	}

	// 读取数据
	cvtBeans := make([]*model.Union_project_bean_mfdcg, 0)
	for _, sheet := range xlfile.Sheets {
		for index, row := range sheet.Rows { // 行
			if index == 0 { // 标题行跳过
				continue
			}
			cells := row.Cells // 行各元素
			cardType, _ := cells[1].Int()
			projectRecord := &model.Union_project_bean_mfdcg{
				QrStrs:    cells[0].String(),
				UnionType: cardType,
			}
			cvtBeans = append(cvtBeans, projectRecord)
		}
	}

	return cvtBeans, nil
}
